package com.zhongen.oa.controller;

import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.plugin.excel.annotation.RequestExcel;
import com.zhongen.admin.api.vo.DeptExcelVo;
import com.zhongen.common.core.util.R;
import com.zhongen.common.log.annotation.SysLog;
import com.zhongen.oa.entity.CustomerInfoEntity;
import com.zhongen.oa.service.CustomerInfoService;
import org.springframework.security.access.prepost.PreAuthorize;
import com.pig4cloud.plugin.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * 客户信息表
 *
 * @author Code Generator
 * @date 2024-03-12 13:06:34
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/customerInfo")
@Tag(description = "customerInfo", name = "客户信息表管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class CustomerInfoController {

	private final CustomerInfoService customerInfoService;

	/**
	 * 分页查询
	 * @param page 分页对象
	 * @param customerInfo 客户信息表
	 * @return
	 */
	@Operation(summary = "分页查询", description = "分页查询")
	@GetMapping("/page")
	@PreAuthorize("@pms.hasPermission('oa_customerInfo_view')")
	public R getCustomerInfoPage(@ParameterObject Page page, @ParameterObject CustomerInfoEntity customerInfo) {
		LambdaQueryWrapper<CustomerInfoEntity> wrapper = Wrappers.lambdaQuery();
		wrapper.like(StrUtil.isNotBlank(customerInfo.getCompany()), CustomerInfoEntity::getCompany,
				customerInfo.getCompany());
		wrapper.like(StrUtil.isNotBlank(customerInfo.getStationName()), CustomerInfoEntity::getStationName,
				customerInfo.getStationName());
		wrapper.like(StrUtil.isNotBlank(customerInfo.getContactName()), CustomerInfoEntity::getContactName,
				customerInfo.getContactName());
		wrapper.like(Objects.nonNull(customerInfo.getPhone()), CustomerInfoEntity::getPhone, customerInfo.getPhone());
		wrapper.like(StrUtil.isNotBlank(customerInfo.getMailbox()), CustomerInfoEntity::getMailbox,
				customerInfo.getMailbox());
		wrapper.like(StrUtil.isNotBlank(customerInfo.getRemark()), CustomerInfoEntity::getRemark,
				customerInfo.getRemark());
		wrapper.eq(StrUtil.isNotBlank(customerInfo.getStatus()), CustomerInfoEntity::getStatus,
				customerInfo.getStatus());
		wrapper.orderByDesc(CustomerInfoEntity::getStatus);
		wrapper.orderByDesc(CustomerInfoEntity::getCreateTime);
		return R.ok(customerInfoService.page(page, wrapper));
	}

	/**
	 * 分页查询
	 * @param page 分页对象
	 * @param customerInfo 客户信息表
	 * @return
	 */
	@Operation(summary = "分页查询", description = "分页查询")
	@GetMapping("/pageApp")
	@PreAuthorize("@pms.hasPermission('oa_customerInfo_view')")
	public R getCustomerInfoPageApp(@ParameterObject Page page, @ParameterObject CustomerInfoEntity customerInfo) {
		LambdaQueryWrapper<CustomerInfoEntity> wrapper = Wrappers.lambdaQuery();
		wrapper.eq(StrUtil.isNotBlank(customerInfo.getStatus()), CustomerInfoEntity::getStatus,
				customerInfo.getStatus());
		wrapper.and(StrUtil.isNotBlank(customerInfo.getCompany()),
				wrapper2 -> wrapper2
					.like(StrUtil.isNotBlank(customerInfo.getCompany()), CustomerInfoEntity::getCompany,
							customerInfo.getCompany())
					.or()
					.like(StrUtil.isNotBlank(customerInfo.getCompany()), CustomerInfoEntity::getStationName,
							customerInfo.getCompany())
					.or()
					.like(StrUtil.isNotBlank(customerInfo.getCompany()), CustomerInfoEntity::getContactName,
							customerInfo.getCompany())
					.or()
					.like(StrUtil.isNotBlank(customerInfo.getCompany()), CustomerInfoEntity::getPhone,
							customerInfo.getCompany())
					.or()
					.like(StrUtil.isNotBlank(customerInfo.getCompany()), CustomerInfoEntity::getMailbox,
							customerInfo.getCompany())
					.or()
					.like(StrUtil.isNotBlank(customerInfo.getCompany()), CustomerInfoEntity::getRemark,
							customerInfo.getCompany()));
		wrapper.orderByDesc(CustomerInfoEntity::getStatus);
		wrapper.orderByDesc(CustomerInfoEntity::getCreateTime);
		return R.ok(customerInfoService.page(page, wrapper));
	}

	/**
	 * 通过id查询客户信息表
	 * @param id id
	 * @return R
	 */
	@Operation(summary = "通过id查询", description = "通过id查询")
	@GetMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('oa_customerInfo_view')")
	public R getById(@PathVariable("id") Long id) {
		return R.ok(customerInfoService.getById(id));
	}

	/**
	 * 新增客户信息表
	 * @param customerInfo 客户信息表
	 * @return R
	 */
	@Operation(summary = "新增客户信息表", description = "新增客户信息表")
	@SysLog("新增客户信息表")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('oa_customerInfo_add')")
	public R save(@RequestBody CustomerInfoEntity customerInfo) {
		return R.ok(customerInfoService.save(customerInfo));
	}

	/**
	 * 修改客户信息表
	 * @param customerInfo 客户信息表
	 * @return R
	 */
	@Operation(summary = "修改客户信息表", description = "修改客户信息表")
	@SysLog("修改客户信息表")
	@PutMapping
	@PreAuthorize("@pms.hasPermission('oa_customerInfo_edit')")
	public R updateById(@RequestBody CustomerInfoEntity customerInfo) {
		return R.ok(customerInfoService.updateById(customerInfo));
	}

	/**
	 * 通过id删除客户信息表
	 * @param ids id列表
	 * @return R
	 */
	@Operation(summary = "通过id删除客户信息表", description = "通过id删除客户信息表")
	@SysLog("通过id删除客户信息表")
	@DeleteMapping
	@PreAuthorize("@pms.hasPermission('oa_customerInfo_del')")
	public R removeById(@RequestBody Long[] ids) {
		return R.ok(customerInfoService.removeBatchByIds(CollUtil.toList(ids)));
	}

	/**
	 * 导出excel 表格
	 * @param customerInfo 查询条件
	 * @param ids 导出指定ID
	 * @return excel 文件流
	 */
	@ResponseExcel
	@GetMapping("/export")
	@PreAuthorize("@pms.hasPermission('oa_customerInfo_export')")
	public List<CustomerInfoEntity> export(CustomerInfoEntity customerInfo, Long[] ids) {
		List<CustomerInfoEntity> list = customerInfoService
			.list(Wrappers.lambdaQuery(customerInfo).in(ArrayUtil.isNotEmpty(ids), CustomerInfoEntity::getId, ids));
		for (CustomerInfoEntity customerInfoEntity : list) {
			String status = customerInfoEntity.getStatus();
			if ("3".equals(status)) {
				customerInfoEntity.setStatus("未联系");
			}
			else if ("2".equals(status)) {
				customerInfoEntity.setStatus("已联系，未拜访");
			}
			else if ("1".equals(status)) {
				customerInfoEntity.setStatus("已拜访");
			}
		}
		return list;
	}

	/**
	 * 导入
	 * @param CustomerInfoEntityList
	 * @param bindingResult
	 * @return
	 */
	@PostMapping("import")
	public R importCustomerInfo(@RequestExcel List<CustomerInfoEntity> CustomerInfoEntityList,
			BindingResult bindingResult) {
		return customerInfoService.importCustomerInfo(CustomerInfoEntityList, bindingResult);
	}

}
